/*  ----------------------------------------------------------------------------
*	This file is part of the AS3This ActionScript 3 library.
*	Author: Steve Jones for G-RAFF <http://www.g-raff.com/>
*	Copyright (c) 2009 raffWorks Ltd.
*	----------------------------------------------------------------------------
*	Please see the notice at the bottom of the code before using or modifying this class.
*/
package com.as3this.ui
{
	import com.as3this.events.AddThisEvent;
	import com.as3this.interfaces.IBaseMenuItem;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;

	public class BaseSharingMenu extends Sprite
	{
		protected var destinations	:Array;
		protected var container		:Sprite;
		protected var shareBtn		:BaseMenuItem;
		protected var config		:Object;
		
		private var _username		:String;
		private var _align	 		:String;
		private var _padding	 	:Object;
		
		private var _canvas			:Sprite;
		
		public static const SHOW_MENU	:String = "showMenu";
		
		public function BaseSharingMenu(canvas:Sprite, destinations:Array, config:Object, showMenuButton:DisplayObject = null)
		{
			_canvas = canvas;
			this.config = config;
			this.destinations = destinations;
					
			// if we have a "share button"
			if (showMenuButton) setShareButton(showMenuButton);
			
			// create the menu container
			container = new Sprite();
			addChild(container);
			
			// initially hidden if there is a "showMenuButton" object
			if (showMenuButton) container.visible = false;
			
			// create the display - will be overridden in subclasses
			createDisplay(destinations);
			
			// and listen for clicks	
			addEventListener(MouseEvent.CLICK, clickHandler, false, 0, true);
		}
				
		/**
		 * assigns any display object to become the button that opens the menu
		 */
		 
		public function setShareButton(btn:DisplayObject) : void
		{
			shareBtn = new BaseMenuItem(0, 0, SHOW_MENU, btn);
			addChild(shareBtn);
			
			// we know the container has to be hidden
			container.visible = false;
		}
		
		public function setPosition(alignment:String, padding:Object = null) : void
		{
			/** 
			 * every chance that the canvas to which the menu has been added
			 * will not actually have a "stage" property yet, so we'll add a listener
			 * and do the work then
			 */
			 _align = alignment;
			 _padding = padding;
			 
			_canvas.addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
		}
		
		protected function addedToStageHandler(event:Event) : void
		{
			// clean up the event listener
			_canvas.removeEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
			
			// now work out the position
			var position:Point = MenuAlign.getPosition(_canvas.stage, _align, container, _padding);
			container.x = position.x;
			container.y = position.y;
		}
		
		public function createDisplay(destinations:Array) : void
		{
			// concrete display code will go here
		}
	
		private function clickHandler(event:MouseEvent) : void
		{
			var item:IBaseMenuItem = event.target as IBaseMenuItem;
			if (item == null) return;
			
			
			if (item.action == SHOW_MENU)
			{
				showMenu();	
			} else
			{
				dispatchEvent(new AddThisEvent(AddThisEvent.SHARE, item.action));
			}
		}
		
		protected function showMenu() : void
		{
			container.visible = true;
			shareBtn.visible = false;
		}	
	}
}

/*
 *    AS3This is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    AS3This is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with AS3This.  If not, see <http://www.gnu.org/licenses/>.
 */